<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/6/15
 * Time: 16:10
 */
namespace Pay\Controller;

use Common\Common\Api\Order;

/**
 * Class ManyiController
 * @package Pay\Controller
 * 凡信非同名卡614
 */
class FfanxinController extends PayController{

    public function pay($array){
        $this->config = M("Channel")->where(array('id'=>$array['channel']))->find();
        $paytypes = transArr(C("PAYTYPES"));

        if(!$this->config){
            $this->showmessage('通道错误!',['code'=>'003']);
        }

        switch($this->config['paytype']){
            //银联在线
            case 13:

                break;
            default:
                $this->showmessage('该通道不支持次方式!',['code'=>'003']);

        }

        $parameter = array(
            'code' => $this->config['code'], // 通道名称
            'title' => $this->config['title'],
            'exchange' => 1, // 金额比例
            'gateway' => $this->config['gateway'],
            'orderid' => createOrderId(),
            'out_trade_id' => I("request.pay_orderid"),
            'body' => 'pay',
            'channel'=>$array
        );

        $return = $this->orderadd( $parameter );

        $data = array();
        $data['merchantId'] = $this->config['mch_id'];
        $data['channel'] = $this->config['appsecret'];
        $data['merOrderNo'] = $return['orderid'];
        $data['amount'] = intval($return['amount'] * 100 );
        $data['notifyUrl'] = $return['notifyurl'];
        $data['returnUrl'] = $return['callbackurl'];
        $data['orderTime'] = date("YmdHis",time());
        $data['settRate'] = '0.0001';
        $data['remitServiceFee'] = $data['amount'] * $data['settRate'];

        ksort($data);

        //echo '<pre/>'; print_r($this->config);
        $str = arrToUrlStr($data)."&key=".$this->config['signkey'];

        $data['sign'] = md5($str);

        $res = curl($this->config['gateway'], arrToUrlStr($data));

        if(!$res){
            $this->showmessage('未获取到数据!',['code'=>'003']);
        }


        //保存请求返回
        M("Order")->where([ 'pay_orderid'=>$return['orderid']])->save(["rd_return"=>$res]);

        $res = json_decode($res, true);

        if($res['resCode'] != '0000'){
            $this->showmessage($res['resMsg'],['code'=>'003']);
        }

        $datainfo = json_decode($res['data'], true);

        /*
         Array
        (
            [resCode] => 0000
            [resMsg] => 成功
            [data] => {"amount":"200","merOrderNo":"201806151635229366098","orderNo":"218061516352272","status":"1","url":"http://www.juchezhan.cn/sand/gw?orderId=218061516352272"}
        )
        Array
        (
            [amount] => 200
            [merOrderNo] => 201806151635229366098
            [orderNo] => 218061516352272
            [status] => 1
            [url] => http://www.juchezhan.cn/sand/gw?orderId=218061516352272
        )
         */


        if($datainfo['url']){
            header("Location: {$datainfo['url']}"); exit;
        }else{
            $this->showmessage('获取支付链接失败!',['code'=>'003']);
        }

    }


    //通知
    public function notifyurl(){
        file_put_contents("a.txt", var_export($_REQUEST,true)."\r\n\r\n");
        file_put_contents("a.txt", var_export($_POST,true)."\r\n\r\n", FILE_APPEND);
        file_put_contents("a.txt", var_export($_GET,true)."\r\n\r\n", FILE_APPEND);
        file_put_contents("a.txt", file_get_contents('php://input')."\r\n\r\n", FILE_APPEND);
        file_put_contents("a.txt", date("Y-m-d H:i:s",time())."\r\n\r\n",FILE_APPEND);

        $p = $_POST;

        /*
        $p = array (
            'merId' => '999100072990037',
            'merOrderId' => '201806151649315574183',
            'orderDate' => '20180615164933',
            'resCdoe' => '0000',
            'resMsg' => '成功',
            'settStat' => '',
            'sign' => 'f1d114c363d27c2b03229b7bb3bbf844',
            'tranAmt' => '200',
        );
        */

        $sign = $p['sign'];
        unset($p['sign']);

        foreach($p as $k=>$v){
            if(!$v){
                unset($p[$k]);
            }
        }

        ksort($p);

        //获取渠道信息
        $this->config = M("Channel")->where(array('code'=>'Ffanxin'))->find();

        $str = arrToUrlStr($p)."&key=".$this->config['signkey'];
        $sign2 = md5($str);

        if($sign != $sign2){
            echo 'sign_wrong';exit;
        }

        if($p['resCdoe'] != "0000"){
            echo "pay_fail"; exit;
        }

        $find = M("Order")->where(array("pay_orderid"=>$p['merOrderId']))->find();
        if(!$find){
            echo 'order_not_found'; exit;
        }

        //保存回调数据
        M("Order")->where(array("pay_orderid"=>$p['merOrderId']))->save(array("server_return"=> file_get_contents('php://input')));

        //处理支付
        $this->EditMoney($find['pay_orderid'],"Fanxin",0);

        //回调成功
        echo 'success'; exit;
    }

    public function callBackUrl(){

        echo '支付成功'; exit;
    }
}